<template>
    <div>
        <el-dialog
            :title="$t('add-install-2')"
            :visible="visibleAdd2"
            class="el-dialog-sm"
            @close="()=>{$store.commit('setVisibleAdd2',false)}">
            <el-form ref="form" :model="form" label-width="200px" >
                <el-row :gutter="24">
                    <el-col :span="20">
                        <el-form-item :label="$t('device-install-page-num')" prop="subStep" :rules="[{ required: form.type=='0'?true:false, message: $t('index_js_notNull')}]">
                            <el-input-number v-model="form.subStep" controls-position="right" placeholder="" :min="1"></el-input-number>
                        </el-form-item>
                        <el-form-item :label="$t('device-install-content')">
                            <el-input type="textarea" v-model="form.description" placeholder=""></el-input>
                        </el-form-item>
                        <el-form-item :label="$t('device-install-img')"  prop="file" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <baseFile v-model="form.file" @change='returnImgUrl'></baseFile>
                            <img class="preview-img" :src="imgBaseUrl" width="100%" height="auto"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setVisibleAdd2',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('save')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import baseFile from '../../../components/common/baseFileInput'
export default {
    components:{
        baseFile
    },
    props: {
        snGuideId:'',
        snGuideStepId:''
    },
    data() {
        return {
            form:{
                snGuideId:'',
                snGuideStepId:'',
                subStep:'',
                description:'',
                file:''
            },
            imgBaseUrl:''
        };
    },
    computed: {
        visibleAdd2(){
            return this.$store.state.sysStore.visibleAdd2
        }
    },
    mounted(){
      
    },
    methods: {
        returnImgUrl(){
            let that = this
            if(this.form.file){
                var reader=new FileReader();
                reader.readAsDataURL(this.form.file);
                reader.onload=function (e) {
                    console.log(e)
                    that.imgBaseUrl=e.target.result;
                }
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                this.commitForm()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
        async commitForm(){
            this.form.snGuideId=this.snGuideId
            this.form.snGuideStepId=this.snGuideStepId
            let res = await this.API.addSubSteps(this.form)
            if(res.success){
                this.$message.success(this.$t('index.control_success'));
            }else{
                if(res.code==0){
                    this.$message.error('子步骤重复');
                }else{
                    this.$message.error(this.$t('index.control_fail'));
                }
            }
            this.$store.commit('setVisibleAdd2',false)
            this.$emit('uploadPage',true)
        }
    }
};
</script>

<style  lang="less">
</style>
